﻿using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.BaseLibrary.Tools.IosTools;

namespace SystemCenterSoa.BaseLibrary.Tools.LogTools
{
    public class Log4Tools
    {
        private  ILog _errorLog;
        private  ILog _innorInfoLog;
        public static ILog ErrorLog
        {
            get {
                var log= DiTools.GetDiObj<Log4Tools>();
                if (log != null) return log._errorLog;
                return null;
            }
        }
        public static ILog InnorInfoLog
        {
            get
            {
                var log = DiTools.GetDiObj<Log4Tools>();
                if (log != null) return log._innorInfoLog;
                return null;
            }
        }
        public Log4Tools()
        {
            //创建日志仓储
            ILoggerRepository repository = LogManager.CreateRepository("CoreLogRepository");
            //读取日志配置文件
            XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
            //初始化错误日志操作对象
            _errorLog = LogManager.GetLogger(repository.Name, "errLog");
            //信息日志
            _innorInfoLog = LogManager.GetLogger(repository.Name, "infoLog");
        }
        /// <summary>
        /// 记录错误之日的方法
        /// </summary>
        /// <param name="throwMsg"></param>
        /// <param name="ex"></param>
        public static void Error(string throwMsg, Exception ex)
        {
            string errorMsg = string.Format("【异常描述】：{0} \r\n【异常类型】：{1} \r\n【异常信息】：{2} \r\n【堆栈调用】：{3}",
               new object[] {
                    throwMsg,
                    ex.GetType().Name,
                    ex.Message,
                    ex.StackTrace });
            errorMsg = errorMsg.Replace("\r\n", "<br>");
            ErrorLog.Error(errorMsg);
        }
        //记录信息日志的方法
        public static void Info(string message)
        {
            InnorInfoLog.Info(string.Format("【日志信息】：{0}", message));
        }

       
    }
}
